/*
* $Id: JournalTable.cpp 152 2010-09-23 18:28:55Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/#include "Common/include.h"
#include "JournalTable.h"
#include "TableNames.h"
#include "../SqlBinder.h"

CJournalTable::CJournalTable()
: CTable(JOURNAL_TABLE_NAME){}
CJournalTable::~CJournalTable(){}

void CJournalTable::CreateTable(IDatabase* pDatabase){
	std::tstring strRequest(_T("CREATE TABLE IF NOT EXISTS "));
	pDatabase->ExecRequest(strRequest + GetNameTable() + 
		_T("(id_journal INTEGER NOT NULL,")\
		_T("id_family INTEGER NOT NULL,")\
		_T("date DATE NOT NULL,")\
		_T("frame INTEGER NOT NULL,")
		_T("strength INTEGER NOT NULL,")\
		_T("is_queen_bee INTEGER NOT NULL,")\
		_T("wax INTEGER NULL,")\
		_T("ambrosia INTEGER NULL,")\
		_T("evacuation INTEGER NULL,")\
		_T("dieting INTEGER NULL,")\
		_T("disease INTEGER NULL,")\
		_T("note TEXT NULL,")\
		_T("id_record INTEGER NULL,")\
		_T("CONSTRAINT fkJournalFamily")\
		_T("	FOREIGN KEY(id_family)")\
		_T("	REFERENCES families(id_family),")\
		_T("CONSTRAINT fkJournalRecord")\
		_T("	FOREIGN KEY(id_record)")\
		_T("	REFERENCES field_values(id_record)")\
		_T("	ON DELETE SET NULL")\
		_T("	ON UPDATE SET NULL,")\
		_T("PRIMARY KEY(id_journal ASC AUTOINCREMENT));"));
}
void CJournalTable::UpdateValue(IDatabase* pDatabase){
	std::tstring strRequest(_T("UPDATE "));
	CSqlBinder binder(strRequest + GetNameTable() + 
					_T(" SET frame=?, strength=?, is_queen_bee=?, wax=?, ambrosia=?,")\
					_T("evacuation=?, dieting=?, disease=?, note=?, id_record=?, date=?")\
					_T(" WHERE id_journal=?"));
	binder.SetValue(0, m_content.s_nFrame);
	binder.SetValue(1, m_content.s_nStrength);
	binder.SetValue(2, m_content.s_QueenBee);
	binder.SetValue(3, m_content.s_nWax);
	binder.SetValue(4, m_content.s_nAmbrosia);
	binder.SetValue(5, m_content.s_nEvacuation);
	binder.SetValue(6, m_content.s_nFeeding);
	binder.SetValue(7, m_content.s_disease);
	binder.SetValue(8, m_content.s_strNote);
	binder.SetValue(9, m_content.s_nIdRecord);
	binder.SetValue(10, m_content.s_date, true);
	binder.SetValue(11, m_content.s_nId);
	pDatabase->ExecRequest(binder.GetRequest());
	m_content.Clear();
}
IResult* CJournalTable::GetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("SELECT * FROM "));
	if(m_content.s_nIdFamily){
		binder.SetRequest(strRequest + GetNameTable() + _T(" WHERE id_family=?"));
		binder.SetValue(0, m_content.s_nIdFamily);
	}else if(m_content.s_nId){
		binder.SetRequest(strRequest + GetNameTable() + _T(" WHERE id_journal=?"));
		binder.SetValue(0, m_content.s_nId);
	}else
		binder.SetRequest(strRequest + GetNameTable());
	m_content.Clear();
	return FillContent(pDatabase->ExecRequest(binder.GetRequest()));
}
void CJournalTable::SetValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("INSERT INTO "));
	binder.SetRequest(strRequest + GetNameTable() + 
					_T("(id_family, date, is_queen_bee, frame, strength, wax,")\
					_T("ambrosia, evacuation, dieting, disease, note, id_record)")\
					_T(" VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));
	binder.SetValue(0, m_content.s_nIdFamily);
	binder.SetValue(1, m_content.s_date, true);
	binder.SetValue(2, m_content.s_QueenBee);
	binder.SetValue(3, m_content.s_nFrame);
	binder.SetValue(4, m_content.s_nStrength);
	binder.SetValue(5, m_content.s_nWax);
	binder.SetValue(6, m_content.s_nAmbrosia);
	binder.SetValue(7, m_content.s_nEvacuation);
	binder.SetValue(8, m_content.s_nFeeding);
	binder.SetValue(9, m_content.s_disease);
	binder.SetValue(10, m_content.s_strNote);
	binder.SetValue(11, m_content.s_nIdRecord);
	pDatabase->ExecRequest(binder.GetRequest());
	strRequest = _T("SELECT MAX(id_journal) as id_journal FROM ");
	MyTable table = pDatabase->ExecRequest(strRequest + GetNameTable());
	if(table.empty())
		m_content.s_nId = 0;
	else
		m_content.s_nId = _ttoi(table[0][0].m_strValueColumn.c_str());
}
void CJournalTable::DeleteValue(IDatabase* pDatabase){
	CSqlBinder binder;
	std::tstring strRequest(_T("DELETE FROM "));
	if(m_content.s_nId){
		strRequest = strRequest + GetNameTable() + _T(" WHERE id_journal=?");
		binder.SetValue(0, m_content.s_nId);
		binder.SetRequest(strRequest);
	}else{
		binder.SetRequest(strRequest + GetNameTable());
	}
	m_content.Clear();
	pDatabase->ExecRequest(binder.GetRequest());
}
IOperation::i_content* CJournalTable::CreateContent(){
	return new content;
}

CJournalTable::content::content(){
	Clear();
}
void CJournalTable::content::Clear(){
	s_nIdFamily = 0;
	s_date = 0;
	s_nFrame = 0;
	s_nStrength = 0;
	s_QueenBee = Journal::DEFAULT;
	s_nWax = 0;
	s_nAmbrosia = 0;
	s_nEvacuation = 0;
	s_nFeeding = 0;
	s_disease = Journal::HEALTHY;
	s_strNote.clear();
	s_nIdRecord = 0;
	s_nId = 0;
}
void CJournalTable::content::Set(const std::vector<CTableItem>& aContent){
	for(std::vector<CTableItem>::const_iterator it = aContent.begin(); it != aContent.end(); it++){
		if(it->m_strNameColumn == _T("id_journal"))
			s_nId = _ttoi(it->m_strValueColumn.c_str());
		if(it->m_strNameColumn == _T("id_family"))
			s_nIdFamily = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("note"))
			s_strNote = it->m_strValueColumn;
		else if(it->m_strNameColumn == _T("is_queen_bee"))
			s_QueenBee = (Journal::queen_bee)_ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("wax"))
			s_nWax = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("id_record"))
			s_nIdRecord = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("ambrosia"))
			s_nAmbrosia = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("evacuation"))
			s_nEvacuation = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("dieting"))
			s_nFeeding = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("frame"))
			s_nFrame = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("strength"))
			s_nStrength = _ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("disease"))
			s_disease = (Journal::disease)_ttoi(it->m_strValueColumn.c_str());
		else if(it->m_strNameColumn == _T("date"))
			s_date = CConvertFunction::ConvertFromDate(it->m_strValueColumn.c_str());
	}
}
void CJournalTable::content::Free(){
	delete this;
}
